package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.cache.util.RedisKeys;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.excption.LogicExcption;
import cn.wolfcode.wolf2w.redis.service.IUserInfRedisService;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository repository;

    @Autowired
    private IUserInfRedisService redisService;

    @Autowired
    private StringRedisTemplate template;

    @Override
    public void save(UserInfo userInfo) {
        userInfo.setId(null);
        repository.save(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public void dilete(String id) {
        repository.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        UserInfo userInfo = repository.findById(id).orElse(null);
        return userInfo;
    }

    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = repository.findByPhone(phone);
        return userInfo!=null;
    }

    @Override
    public UserInfo getByUsername(String name) {
        repository.findByNickname(name);
        return null;
    }

    @Override
    public List<UserInfo> list() {
        return repository.findAll();
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        try {
            AssertUtil.hasLength(phone, "电话号码不能为空");
            AssertUtil.hasLength(nickname, "昵称不能为空");
            AssertUtil.hasLength(password, "密码不能为空");
            AssertUtil.hasLength(rpassword, "重复密码不能为空");
            AssertUtil.hasLength(verifyCode, "验证码不能为空");
            AssertUtil.isEquals(password, rpassword, "两次输入的密码不一致");

            if (this.checkPhone(phone)) {
                throw new LogicExcption("手机号已经存在");
            }

            String code = redisService.getVerifyCode(phone);
            if (!verifyCode.equals(code)){
                throw new LogicExcption("验证码不正确");
            }
            UserInfo user = new UserInfo();
            user.setCity("");
            user.setEmail("");
            user.setHeadImgUrl("/images/default.jpg");
            user.setInfo("");
            user.setLevel(1);
            user.setNickname(nickname);
            user.setPassword(password);
            user.setPhone(phone);
            repository.save(user);

        } catch(Exception e) {
            e.printStackTrace();
            throw new LogicExcption(e.getMessage());
        }
    }

    @Override
    public UserInfo login(String phone, String password) {
        UserInfo user = repository.findByPhone(phone);
        if (user==null){
            throw new LogicExcption("账号不正确");
        }
        AssertUtil.isEquals(password, user.getPassword(), "密码不正确");
        return user;
    }

    @Override
    public List<String> getUserFavor(String uid) {
        String uidKey = RedisKeys.USER_FAVOR.join(uid);
        List<String> sids = JSON.parseArray(template.opsForValue().get(uidKey), String.class);
        return sids;
    }

    @Override
    public List<UserInfo> getByCity(String keyword) {
        return repository.findByCity(keyword);
    }
}
